﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simple.Services.System;

namespace Simple.Services
{
    public class CourseTypeService : BusinessBaseService
    {
        private readonly SimpleDbContext _context;
        public CourseTypeService(SimpleDbContext context,
            ISimpleService services) : base(services)
        {
            _context = context;
        }

        public async Task<List<CourseTypeModel>> GetAsync()
        {
            var lstCourseType = await (await OrganizationFilter(_context.Set<CourseType>())).ToListAsync();
            return MapperHelper.Map<List<CourseTypeModel>>(lstCourseType);
        }

        public async Task<PageResultModel<CourseTypeModel>> GetPageAsync(CourseTypePageInputModel input)
        {
            var result = new PageResultModel<CourseTypeModel>();
            var query = await OrganizationFilter(_context.Set<CourseType>().AsQueryable());

            // 获取总数量
            result.TotalRows = await query.CountAsync();

            // 分页查询
            query = query.OrderBy(u => u.Sort).Page(input.PageNo, input.PageSize);
            var lstCourseType = await query.ToListAsync();
            var lstCourseTypeDto = MapperHelper.Map<List<CourseTypeModel>>(lstCourseType);

            result.Rows = lstCourseTypeDto;

            result.SetPage(input);
            result.CountTotalPage();

            return result;
        }

        public async Task<int> AddAsync(CourseTypeInputModel model)
        {
            if (await _context.Set<CourseType>().AnyAsync(u => u.Name == model.Name))
            {
                throw AppResultException.Status409Conflict("存在相同课程分类");
            }

            var courseType = MapperHelper.Map<CourseType>(model);
            var entityChild = await _context.AddAsync(courseType);

            return await _context.SaveChangesAsync();
        }

        public async Task<int> UpdateAsync(CourseTypeInputModel model)
        {
            if (await _context.Set<CourseType>().AnyAsync(u => u.Id != model.Id && u.Name == model.Name))
            {
                throw AppResultException.Status409Conflict("存在相同课程分类");
            }

            var courseType = await _context.Set<CourseType>()
                .Where(u => model.Id == u.Id)
                .FirstOrDefaultAsync();

            if (courseType == null)
            {
                throw AppResultException.Status404NotFound("找不到课程分类，更新失败");
            }
            MapperHelper.Map(model, courseType);

            var entityChild = _context.Update(courseType);
            int ret = await _context.SaveChangesAsync();

            if (ret == 0)
            {
                throw AppResultException.Status200OK("更新记录数为0");
            }
            return ret;
        }

        public async Task<int> DeleteAsync(IEnumerable<Guid> ids)
        {
            var lstCourseType = await _context.Set<CourseType>()
                .Where(u => ids.Contains(u.Id))
                .ToListAsync();

            _context.RemoveRange(lstCourseType);
            return await _context.SaveChangesAsync();
        }
    }


}
